package net.wangds.yunz.auth.handler;

import net.wangds.log.helper.LogHelper;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 接口无权限处理类
 **/
@Component
public class JwtAccessDeniedHandler implements AccessDeniedHandler {

    /**
     * Handles an access denied failure.
     *
     * @param request               that resulted in an <code>AccessDeniedException</code>
     * @param response              so that the user agent can be advised of the failure
     * @param accessDeniedException that caused the invocation
     */
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException {
        LogHelper.error("----------------------access denied handler", accessDeniedException);
        response.sendError(HttpStatus.NOT_ACCEPTABLE.value(), accessDeniedException.getLocalizedMessage());
    }
}
